﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using DevComponents.DotNetBar;
using PMQLCHH.BUS;
using PMQLCHH.DTO;

namespace PMQLCHH.GUI
{
    public partial class POPUPMATHANG : DevComponents.DotNetBar.Office2007Form
    {
        public delegate void GetArray(ArrayList ArrDSMH);
        public GetArray get_MaMH; 

        public POPUPMATHANG()
        {
            InitializeComponent();
        }
        //danh sach mat hang da chon
        private ArrayList m_ArrDSChon;

        //events
        private void POPUPKHACHHANG_Load(object sender, EventArgs e)
        {
            NapDanhSachMatHang();
            NapTiemKiemLoaiMatHang();
            m_ArrDSChon = new ArrayList();
        }

        private void btn_thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_timkiem_Click(object sender, EventArgs e)
        {
            KetQuaTimKiem();
        }

        private void btn_hoantat_Click(object sender, EventArgs e)
        {
            //ArrayList arrtemp = new ArrayList();
            //arrtemp.Add("MH1000001");
            //arrtemp.Add("MH1000002");
            //get_MaMH(arrtemp);
            get_MaMH(m_ArrDSChon);
            this.Close();
        }

        private void dtg_DanhSachMatHang_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (DaBamNutThem(e))
            {
                //Them
                int iDong = e.RowIndex;
                m_ArrDSChon.Add(dtg_DanhSachMatHang.Rows[iDong].Cells["MaMatHang"].Value.ToString());
                CapNhatGioHang();
            }
        }
        private void txt_timkiem_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btn_timkiem.PerformClick();
            }
        }

        //methods
        /// <summary>
        /// Nap sanh sach mat hang len man hinh
        /// </summary>
        ///
        public void NapDanhSachMatHang()
        {
            MatHangBUS MHBus = new MatHangBUS();
            dtg_DanhSachMatHang.DataSource = MHBus.LayDanhSachMatHangDeBan();
        }
        /// <summary>
        /// kiem tra phan duoc bam vao la Nut xoa them
        /// </summary>
        ///
        private bool DaBamNutThem(DataGridViewCellEventArgs cellEvent)
        {
            if (dtg_DanhSachMatHang.Columns[cellEvent.ColumnIndex] is DataGridViewButtonColumn && cellEvent.RowIndex != -1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// cap nhat trang thai cua gio hang
        /// </summary>
        ///
        public void CapNhatGioHang()
        {
            int count = m_ArrDSChon.Count;
            lbl_GioHang.Text = "Có " + count.ToString() + " mặt hàng được chọn";
        }

        /// <summary>
        /// Tim Kiem San Pham
        /// </summary>
        /// 
        public void KetQuaTimKiem()
        {
            string strTuKhoa = txt_timkiem.Text;
            int iLoaiMatHang = int.Parse(cbb_tiemkiem.SelectedValue.ToString());
            MatHangBUS MHbus = new MatHangBUS();
            dtg_DanhSachMatHang.DataSource = MHbus.TimKiemMatHangDeBan(strTuKhoa, iLoaiMatHang);
        }

        /// <summary>
        /// Nap Danh sach loai mat hang cho chuc nang tim kiem 
        /// </summary>
        /// 
        public void NapTiemKiemLoaiMatHang()
        {
            try
            {
                MatHangBUS MHBus = new MatHangBUS();
                DataTable dt = MHBus.LayDanhSachLoaiMatHang();
                int iSoDong = dt.Rows.Count;
                DataTable dtV = new DataTable();
                dtV.Columns.Add("MaLoaiMatHang");
                dtV.Columns.Add("TenLoaiMatHang");
                DataRow newCustomersRow = dtV.NewRow();
                newCustomersRow["MaLoaiMatHang"] = 0;
                newCustomersRow["TenLoaiMatHang"] = "Tất Cả";
                dtV.Rows.Add(newCustomersRow);
                if (iSoDong > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        string strTemps = dr["TenLoaiMatHang"].ToString();
                        int iTemp = (int)dr["MaLoaiMatHang"];
                        newCustomersRow = dtV.NewRow();
                        newCustomersRow["MaLoaiMatHang"] = iTemp;
                        newCustomersRow["TenLoaiMatHang"] = strTemps;
                        dtV.Rows.Add(newCustomersRow);
                    }
                }
                cbb_tiemkiem.DataSource = dtV;
                cbb_tiemkiem.ValueMember = "MaLoaiMatHang";
                cbb_tiemkiem.DisplayMember = "TenLoaiMatHang";
                cbb_tiemkiem.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}